package com.yy.smartBI.mqtest;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import org.apache.commons.lang3.concurrent.ConcurrentUtils;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.TreeMap;
import java.util.concurrent.CopyOnWriteArraySet;

public class DieLetterConsumer {
    private final static String QUEUE_NAME1 = "m1";
    private final static String QUEUE_NAME2 = "m2";


    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel1 = connection.createChannel();
        Channel channel2 = connection.createChannel();
        //声明死信交换机
        channel1.exchangeDeclare("e2", "direct");
        channel2.exchangeDeclare("e2", "direct");
        //声明死信队列参数
        HashMap<String, Object> args1 = new HashMap<>();
        args1.put("x-dead-letter-exchange", "e2");
        args1.put("x-dead-letter-routing-key", "worker1");

        HashMap<String, Object> args2 = new HashMap<>();
        args2.put("x-dead-letter-exchange", "e2");
        args2.put("x-dead-letter-routing-key", "worker2");

        //声明交换机
        channel1.exchangeDeclare("e1", "direct");
        //声明队列
        channel1.queueDeclare(QUEUE_NAME1, true, false, false, args1);
        //绑定队列到交换机
        channel1.queueBind(QUEUE_NAME1, "e1", "exc1");

        //声明交换机 同上
        channel2.exchangeDeclare("e1", "direct");
        channel2.queueDeclare(QUEUE_NAME2, true, false, false, args2);
        channel2.queueBind(QUEUE_NAME2, "e1", "exc2");


        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        DeliverCallback deliverCallback1 = (consumerTag, delivery) -> {
            try {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            if (message.equals("hello exc1")) {
                throw new InterruptedException("hello exc1");
            }
            System.out.println(" [x]exc1 Received1 '" + message + "'");

                //模拟消息处理
                Thread.sleep(1000);
                //如果消息处理失败，将消息发送到死信队列
                //确认消息
                channel1.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            } catch (InterruptedException e) {
                //如果消息处理失败，将消息发送到死信队列
                channel1.basicNack(delivery.getEnvelope().getDeliveryTag(), false, false);
            }
        };

        DeliverCallback deliverCallback2 = (consumerTag, delivery) -> {
            try {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            if (message.equals("hello exc2")) {
                throw new InterruptedException("hello exc2");
            }
            System.out.println(" [x]exc2 Received1 '" + message + "'");
                //模拟消息处理
                Thread.sleep(1000);


                //确认消息
                channel2.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            } catch (InterruptedException e) {
                //如果消息处理失败，将消息发送到死信队列
                channel2.basicNack(delivery.getEnvelope().getDeliveryTag(), false, false);
            }
        };

        channel1.basicConsume(QUEUE_NAME1, false, deliverCallback1, consumerTag -> { });
        channel2.basicConsume(QUEUE_NAME2, false, deliverCallback2, consumerTag -> { });
    }
}
